package io.yizoo.business.controller;

import io.elm.core.CoreController;
import io.elm.core.HMapper;
import io.yizoo.business.entity.biz_plm_plan;
import io.yizoo.business.entity.biz_plm_plan_stage;
import io.yizoo.business.entity.biz_plm_plan_task;
import io.yizoo.business.entity.biz_plm_product;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/plm/plan/task")
public class plm_plan_taskController extends CoreController {

    @Override
    protected void Init() {
        PowerEntity = biz_plm_plan_task.class;
        PowerUse = EnumSet.of(Power.IDDelete, Power.IDAny, Power.IDGetSingle,Power.IDUpsert);
    }

    @PostMapping("/GetTree")
    public Object GetTree(@RequestBody String jsonStr) throws Exception {
        Map<String, Object> factor = Ctor.DMaker(jsonStr).Selector
                .From(biz_plm_plan_task.class)
                .Where(" and (pid IS NULL OR pid = '' OR pid = '0')")
                .IsWhere("and plan_code = #{plan_code}","plan_code")
                .IsWhere("and name like CONCAT('%', #{name}, '%')","name")
                .IsPage()
                .GetFactor();
        HMapper.PageData data = Ctor.HMapper.GetTree(factor);
        return JsonResult(data);
    }

    @PostMapping("/GetPath")
    public Object GetPath(@RequestBody String jsonStr) {
        Object data = Ctor.HMapper.GetPath(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_plm_plan_task.class)
                        .Select("id,pid,name")
                        .IsWhere("and plan_code = #{plan_code}","plan_code")
                        .IsWhere("and id=#{id}","id")
                        .GetFactor());
        return JsonResult(data);
    }

    @PostMapping("/GetProduct")
    public Object GetProduct(@RequestBody String jsonStr) throws Exception {
        List<?> ret = Ctor.CoreMapper.USelect(Ctor.DMaker(jsonStr).Selector
                .Select("sys_code_key,name")
                .From(biz_plm_product.class)
                .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) throws Exception {
        List<?> ret = Ctor.CoreMapper.USelect(Ctor.DMaker(jsonStr).Selector
                .Select("*,ifnull(a.name,t.plan_code) as plan_name")
                .From(biz_plm_plan_task.class,"t")
                .LeftJoin(biz_plm_plan.class,"a","a.sys_code_key=t.plan_code")
                .Where("and t.status != '已完成' ")
                .IsWhere("and plan_code = #{plan_code}","plan_code")
                .IsWhere("and t.assignee_id = #{user}","user")
                .IsWhere("and t.name like CONCAT('%', #{name}, '%')","name")
                .OrderBy("t.jh_end_time")
                .IsPage()
                .GetFactor());
        return JsonResultPage(ret);
    }
}
